{{ extend '../_layout/layout.html' }}

{{ block 'header'}}
<link rel="stylesheet" href="/public/css/profile.css">
{{ /block }}

{{ block 'content' }}
<div class="container">
    <div class="clearfix">
        <ul class="side_nav">
            <li class="disabled">用户设置</li>
            <li><a href="/settings/profile">基本信息</a></li>
            <li><a href="/settings/admin" class="nav_active">账户设置</a></li>
        </ul>
        <div class="content">
            <h2>修改密码</h2>
            <hr>
            <form id="admin_form">
                <label for="curPassword">当前密码</label>
                <input type="password" name="curPassword" id="curPassword" class="input" required>
                <label for="newPassword">新的密码</label>
                <input type="password" name="newPassword" id="newPassword" class="input" required>
                <label for="dePassword">确认密码</label>
                <input type="password" name="dePassword" id="dePassword" class="input" required>
                <button class="btn">保存</button>
            </form>
            <h2>注销账号</h2>
            <hr>
    
            <div class="delete">
                <p>一旦注销，不可找回，请谨慎操作</p>
                <a href="javascript:;" class="btn" id="delete">注销</a>
            </div>
        </div>
    </div>
   
</div>
{{ /block }}

{{ block 'script'}}
<script>
    // 为表单绑定一个submit事件
    $('#admin_form').on('submit', function (e) {
        // 阻止表单的默认事件
        e.preventDefault()
        let formData = $(this).serialize()
        let newPw = $('#newPassword').val()
        let dePw = $('#dePassword').val()
        if (newPw === dePw) {
            $.ajax({
                url: '/settings/admin',
                type: 'post',
                data: formData,
                dataType: 'json',
                success: function (data) {
                    if (data.err_code === 0) {
                        // 提示修改成功，并重新登录
                        window.alert('密码修改成功')
                        window.location.href = '/login'
                    } else {
                        window.alert(data.msg)
                    }
                }
            })
        } else {
            window.alert('新密码与确认密码不一致')
        }

    })

    $('#delete').click((e) => {
        e.preventDefault()
        const con = window.confirm('是否要注销账号 {{ user.email }}? 此操作不可恢复 ')
        if(con){
            $.ajax({
                url: '/settings/admin',
                type: 'delete',
                dataType: 'json',
                success: function (data) {
                    if(data.err_code === 0){
                        // 显示注销成功，并跳转到主页
                        window.alert('注销成功！')
                        window.location.href = '/'
                    }else{
                        window.alert('服务器繁忙！')
                    }
                }
            })
        }
    })
</script>
{{ /block }}